package com.xubeizhi.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xubeizhi.common.Result;
import com.xubeizhi.entity.Category;
import com.xubeizhi.mapper.CategoryMapper;
import com.xubeizhi.service.CategoryService;
import com.xubeizhi.utils.DataUtils;
import com.xubeizhi.vo.CategoryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    /**
     * 新增分类
     * @param category
     * @return
     */
    @Override
    public Result create(Category category) {
        BeanUtils.copyProperties(category,Category.class);

        category.setCreateTime(DataUtils.getDateNow());
        category.setUpdateTime(DataUtils.getDateNow());
        int rows = baseMapper.insert(category);
        return rows > 0 ? Result.success(0,"操作成功",null) : Result.error(1,"操作失败",null);
    }

    /**
     * 获取分类列表
     * @param categoryVo
     * @return
     */
    @Override
    public Result getCategory(CategoryVo categoryVo) {
        List<CategoryVo> list = baseMapper.selectCategoryList(categoryVo);
        if (list != null){
            return Result.success(0,"操作成功",list);
        }
        return Result.error(1,"操作失败",null);
    }

    /**
     * 根据id获取文章分类详情
     *
     * @param id
     * @return
     */
    @Override
    public Result getCategoryById(Integer id) {
        CategoryVo categoryVo = baseMapper.selectCategoryById(id);
        if (categoryVo != null){
            return Result.success(0,"操作成功",categoryVo);
        }
        return Result.error(1,"操作失败",null);
    }

    /**
     * 更新文章分类
     * @param category
     * @return
     */
    @Override
    public Result updateCategory(Category category) {
        category.setUpdateTime(DataUtils.getDateNow());
        int rows = baseMapper.updateById(category);
        return rows > 0 ? Result.success(0,"操作成功",category) : Result.error(1,"操作失败",null);
    }

    /**
     * 删除文章分类
     * @param id
     * @return
     */
    @Override
    public Result deleteCategoryById(Integer id) {
        int rows = baseMapper.deleteById(id);
        return rows > 0 ? Result.success(0,"操作成功",null) : Result.error(1,"操作失败",null);
    }


}
